<?php
/**
 * Zend Image Manipulation Module
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.

 * @category   Zimm
 * @package    Zimm
 * @copyright  Copyright (c) 2012 Kristoffer Ingemansson
 * @license    New BSD License
 * @version    $Id: Mirror.php 4 2012-04-14 12:59:16Z kristoffer.ingemansson@gmail.com $
 */

class Zimm_Model_DbTable_Instruction_Mirror extends Zimm_Model_DbTable_Instruction_Abstract
{
    /**
     * Mirror image vertically, horizontally or both.
     *
     * @param resource $image GD resource
     * @param string $savePath Target save path
     * @param array $params Instruction arguments
     * @return boolean Indicates if image was altered
     */
    protected function _process(&$image, $savePath, $params)
    {
        $vertical = isset($params['vertical']);
        $horizontal = isset($params['horizontal']);
        if ($vertical || $horizontal) {
            $toWidth = $width = imagesx($image);
            $toHeight = $height = imagesy($image);
            $x = 0;
            $y = 0;
            if ($vertical) {
                $y = $height;
                $toHeight = -$height;
            }
            if ($horizontal) {
                $x = $width;
                $toWidth = -$width;
            }
            $im_dst = imagecreatetruecolor($width, $height);
            imagecopyresampled($im_dst, $image, 0, 0, $x, $y, $width, $height, $toWidth, $toHeight);
            imagedestroy($image);
            $image = $im_dst;
            return true;
        }
        return false;
    }
}